package com.wwc.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwc.business.controller.dto.train.TrainDeleteDTO;
import com.wwc.business.controller.dto.train.TrainInsertDTO;
import com.wwc.business.controller.dto.train.TrainSelectListDTO;
import com.wwc.business.controller.dto.train.TrainUpdateDTO;
import com.wwc.business.controller.vo.TrainSelectListVO;
import com.wwc.business.entity.Train;
import com.wwc.business.mapper.TrainMapper;
import com.wwc.business.service.ITrainService;
import com.wwc.common.exception.BusinessException;
import com.wwc.common.exception.BusinessExceptionEnum;
import com.wwc.common.response.PageResponse;
import com.wwc.common.utils.SnowUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TrainServiceImpl extends ServiceImpl<TrainMapper, Train> implements ITrainService{
	@Resource
	private TrainMapper trainMapper;

	@Override
	public void insert(TrainInsertDTO trainInsertDTO){
		// 根据查询条件获取火车列表
		QueryWrapper<Train> trainQueryWrapper = new QueryWrapper<>();
		trainQueryWrapper.eq("code", trainInsertDTO.getCode());
		List<Train> trainList = trainMapper.selectList(trainQueryWrapper);
		// 如果火车编号已存在则抛出异常
		if(CollUtil.isNotEmpty(trainList)){
			throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CODE_UNIQUE_ERROR);
		}

		// 新增火车
		Train train = BeanUtil.copyProperties(trainInsertDTO, Train.class);
		train.setId(SnowUtil.getSnowflakeNextId());
		train.setCreateTime(new Date());
		train.setUpdateTime(new Date());
		trainMapper.insert(train);
	}

	@Override
	public void update(TrainUpdateDTO trainUpdateDTO){
		// 判断想要修改的数据是否存在
		Train t = trainMapper.selectById(trainUpdateDTO.getId());
		if(ObjectUtil.isNull(t)){
			throw new BusinessException(BusinessExceptionEnum.MODIFIED_DATA_DOES_NOT_EXIST);
		}

		// 根据查询条件查询火车列表
		QueryWrapper<Train> trainQueryWrapper = new QueryWrapper<>();
		trainQueryWrapper.eq("code", trainUpdateDTO.getCode());
		trainQueryWrapper.ne("id", trainUpdateDTO.getId());
		List<Train> trainList = trainMapper.selectList(trainQueryWrapper);
		// 根据车次编号查询如果为空 或者 车次编号和要修改的车次是同一条数据，反之抛出异常
		if(CollUtil.isNotEmpty(trainList)){
			throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CODE_UNIQUE_ERROR);
		}

		// 修改火车
		Train train = BeanUtil.copyProperties(trainUpdateDTO, Train.class);
		train.setUpdateTime(new Date());
		trainMapper.updateById(train);
	}

	@Override
	public void delete(TrainDeleteDTO trainDeleteDTO){
		// 判断想要删除的数据是否存在
		Train t = trainMapper.selectById(trainDeleteDTO.getId());
		if(ObjectUtil.isNull(t)){
			throw new BusinessException(BusinessExceptionEnum.MODIFIED_DELETE_DATA_DOES_NOT_EXIST);
		}

		// 删除火车
		trainMapper.deleteById(trainDeleteDTO.getId());
	}

	@Override
	public PageResponse<TrainSelectListVO> selectListPage(TrainSelectListDTO trainSelectListDTO){
		// 根据分页配置和查询条件获取火车分页列表
		Page<Train> page = new Page<>(trainSelectListDTO.getPage(), trainSelectListDTO.getSize());
		QueryWrapper<Train> trainQueryWrapper = new QueryWrapper<>();
		trainQueryWrapper.orderByAsc("code");
		Page<Train> stationPage = trainMapper.selectPage(page, trainQueryWrapper);

		// 返回自定义分页配置类
		PageResponse<TrainSelectListVO> pageResponse = new PageResponse<>();
		pageResponse.setTotal(stationPage.getTotal());
		pageResponse.setList(BeanUtil.copyToList(stationPage.getRecords(), TrainSelectListVO.class));
		return pageResponse;
	}

	@Override
	public List<TrainSelectListVO> selectList(){
		// 根据查询条件获取火车列表
		QueryWrapper<Train> trainQueryWrapper = new QueryWrapper<>();
		trainQueryWrapper.orderByAsc("code");
		List<Train> trainList = trainMapper.selectList(trainQueryWrapper);

		// 返回自定义VO
		return BeanUtil.copyToList(trainList, TrainSelectListVO.class);
	}
}
